package com.acme.bankapp.domain.bank;

import java.io.Serializable;
import com.acme.bankapp.domain.bank.exceptions.NotEnoughFundsException;

/**
 * Created by anton on 8/18/14.
 */
public abstract class AbstractAccount implements Account, Serializable {
    protected double balance;

    public AbstractAccount(double balance) {
        if ( balance < 0 ) {
            throw new IllegalArgumentException("Balance must be positive.");
        }
        this.balance = balance;
    }

    @Override
    public double getBalance() {
        return this.balance;
    }

    @Override
    public void deposit(double x) {
        this.balance += x;
    }

    @Override
    abstract public void withdraw(double x) throws NotEnoughFundsException;

    @Override
    abstract public double maximumAmountToWithdraw();

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof AbstractAccount)) return false;

        AbstractAccount that = (AbstractAccount) o;

        if (Double.compare(that.balance, balance) != 0) return false;

        return true;
    }

    @Override
    public int hashCode() {
        long temp = Double.doubleToLongBits(balance);
        return (int) (temp ^ (temp >>> 32));
    }

    @Override
    abstract public String toString();

    public double decimalValue() {
        return Math.round(this.getBalance());
    }
}
